﻿using System.Web.Script.Serialization;

namespace LaoLuo
{
    using System.IO;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Xml.Serialization;

    /// <summary>
    /// 序列化辅助类
    /// </summary>
    public static class SerializationHelper
    {
        /// <summary>
        /// 使用DataContractJsonSerializer将对象序列化为json字符串
        /// </summary>
        /// <typeparam name="T">要序列化的对象的类型</typeparam>
        /// <param name="obj">要序列化的对象</param>
        /// <returns>json字符串</returns>
        public static string ToJson<T>(this T obj)
        {
            using (var ms = new MemoryStream())
            {
                var ser = new DataContractJsonSerializer(obj.GetType());
                ser.WriteObject(ms, obj);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }

        /// <summary>
        /// 使用JavaScriptSerializer将对象序列化为json字符串
        /// </summary>
        /// <typeparam name="T">要序列化的对象的类型</typeparam>
        /// <param name="obj">要序列化的对象</param>
        /// <returns>json字符串</returns>
        public static string ToJavascriptJson<T>(this T obj)
        {
            return new JavaScriptSerializer().Serialize(obj);
        }

        /// <summary>
        /// 将字符串反序列化为对象
        /// </summary>
        /// <typeparam name="T">要反序列化成为的类型</typeparam>
        /// <param name="str">字符串</param>
        /// <returns>反序列化后的对象</returns>
        public static T FromJson<T>(this string str)
        {
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(str.ToCharArray())))
            {
                var ser = new DataContractJsonSerializer(typeof(T));
                return (T)ser.ReadObject(ms);
            }
        }

        /// <summary>
        /// 将对象序列化为xml字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ToXml<T>(this T o)
        {
            string retVal;
            using (var ms = new MemoryStream())
            {
                var xs = new XmlSerializer(typeof(T));
                xs.Serialize(ms, o);
                ms.Flush();
                ms.Position = 0;
                var sr = new StreamReader(ms, Encoding.UTF8);
                retVal = sr.ReadToEnd();
            }
            return retVal;
        }

        /// <summary>
        /// 将xml字符串反序列化为对象
        /// </summary>
        /// <typeparam name="T">反序列化为此类型的对象</typeparam>
        /// <param name="s">xml字符串</param>
        /// <param name="encoding">编码,不指定为Encoding.Default</param>
        /// <returns>反序列化后的对象</returns>
        public static T FromXml<T>(this string s, Encoding encoding = null)
        {
            encoding = encoding ?? Encoding.Default;

            using (var streamReader = new StreamReader(new MemoryStream(encoding.GetBytes(s)), encoding))
            {
                var xs = new XmlSerializer(typeof(T));
                return (T)xs.Deserialize(streamReader);
            }
        }
    }
}
